﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace wontec_claw
{
    public partial class sersonview : Form
    {


        private static Icon newIcon = new Icon(new MemoryStream(new byte[]{
0x00,0x00,0x01,0x00,0x01,0x00,0x18,0x18,0x00,0x00,0x01,0x00,0x20,0x00,0xE0,0x00,
0x00,0x00,0x16,0x00,0x00,0x00,0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A,0x00,0x00,
0x00,0x0D,0x49,0x48,0x44,0x52,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x18,0x04,0x03,
0x00,0x00,0x00,0x12,0x59,0x20,0xCB,0x00,0x00,0x00,0x01,0x73,0x52,0x47,0x42,0x01,
0xD9,0xC9,0x2C,0x7F,0x00,0x00,0x00,0x09,0x70,0x48,0x59,0x73,0x00,0x00,0x0B,0x13,
0x00,0x00,0x0B,0x13,0x01,0x00,0x9A,0x9C,0x18,0x00,0x00,0x00,0x15,0x50,0x4C,0x54,
0x45,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x12,0x01,0x00,0xA4,0x00,0x00,0x00,0x07,0x74,0x52,
0x4E,0x53,0x00,0x6D,0xCE,0xFF,0xA5,0x76,0x50,0xA4,0x93,0xB6,0x98,0x00,0x00,0x00,
0x51,0x49,0x44,0x41,0x54,0x78,0x9C,0x63,0x60,0x20,0x05,0x08,0x29,0x22,0xD8,0x8C,
0xC6,0xC6,0x02,0x60,0x06,0x8B,0x31,0x10,0x98,0x06,0x1B,0x1B,0x3B,0xC0,0x39,0xC9,
0x10,0x0E,0x8A,0x32,0x34,0x03,0xB0,0x1A,0x05,0x36,0x00,0x64,0x94,0x31,0xCC,0x34,
0x90,0x51,0x50,0x0E,0x4E,0xA3,0x60,0xA6,0x28,0x21,0xC9,0x0A,0x1B,0x1B,0xC2,0xDD,
0x66,0x6C,0x1C,0x8A,0x70,0x9B,0xB1,0x71,0x18,0xDC,0x6D,0x70,0x65,0x58,0x0C,0x20,
0x00,0x00,0xC6,0x0E,0x10,0x12,0x61,0xE9,0x71,0x8D,0x00,0x00,0x00,0x00,0x49,0x45,
0x4E,0x44,0xAE,0x42,0x60,0x82}));

        List<byte> recvData;
        Action<byte[]> uartsender;
        public Action<byte[]> uartcmdsender;
        pipeline Pipe;
        sersonhelper Helper;
        int selectid;
        int sersonvalue = 0;
        void sendstr(string str)
        {
            uartsender(System.Text.Encoding.Default.GetBytes(str));
        }

        public sersonview(Action<byte[]> send,List<byte> recv)
        {
            recvData = recv;
            uartsender = send;
            InitializeComponent();
            Icon = newIcon;
            Helper = new sersonhelper();
            Helper.AddCmd('t', ">%2x%c%2x", ">fet",(key,id,data)=> {
                selectid = data[0];
                Invoke(new MethodInvoker(() => {
                    info_id.Text = "" + selectid;
                    SendData(Helper['B', selectid] as string,true);
                }));
            });
            Helper.AddCmd('$', ">%2x%c%2x", ">fet", (key, id, data) => {
                int targetid = data[0];
                Invoke(new MethodInvoker(() => {
                    info_id.Text = "" + targetid;
                    listBox1.Items.Add($"{info_id.Text}");
                    if (listBox1.SelectedIndex == -1)
                        listBox1.SelectedIndex = 0;
                    
                }));
            });
            Helper.AddCmd('B', ">%2x%c%4x", ">%2x%c", (key, id, data) => {
                sersonvalue = data[0];
                Invoke(new MethodInvoker(() => {
                    info_value.Text = "" + sersonvalue;
                }));
            });
            Helper.AddCmd('C', ">%2x%c", ">%2x%c%4x");
            Helper.AddCmd('E', ">%2x%c", ">%2x%c");
            Helper.AddCmd('T', ">%2x%c", ">%2x%c%2x");
            Helper.AddCmd('i', ">%2x%c", ">%2x%c%2x",(key,id,data)=> {
                Invoke(new MethodInvoker(() => {
                    listBox1.Items[listBox1.Items.IndexOf(""+selectid)]=""+id;
                    selectid = id;
                }));
                
            });
            Helper.AddCmd('S', ">%2x%c%2x%2x", ">%2x%c%2x%2x");
            Pipe = new pipeline(recvData, (a) => {
                var data=Helper[a];
                Invoke(new MethodInvoker(() => {
                    log.AppendText("[" + DateTime.Now.ToString("HH:mm:ss") + "] 接收: "+a);
                }));
            }, sendstr);
            //var P = new pipeline(recvData,(a)=> { },System.Text.Encoding.Default.GetBytes() uartsender);

        }

        void SendData(string str,bool ack=false)
        {
            log.AppendText("[" + DateTime.Now.ToString("HH:mm:ss") + "] 发送: "+str);
            Pipe.SendData(str, ack ? str.Substring(0, 4):"");
        }

        private void rd_id_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            SendData(Helper['t', selectid] as string);
        }

        private void wr_id_Click(object sender, EventArgs e)
        {
            try {
                SendData(Helper['T', selectid, int.Parse(listBox1.Text)] as string);
            }
            catch { }
        }

        private void rd_value_Click(object sender, EventArgs e)
        {
            SendData(Helper['B', selectid] as string);
        }

        private void wr_value_Click(object sender, EventArgs e)
        {
            try{
                SendData(Helper['C', selectid, int.Parse(info_value.Text)] as string);
            }
            catch { }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            SendData(Helper['E', selectid] as string);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectid=int.Parse(listBox1.SelectedItem as string);
            info_id.Text = ""+selectid;
            SendData(Helper['B', selectid] as string, true);
        }
    }
}
